<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程名称" prop="instanceName">
        <el-input v-model="queryParams.instanceName" placeholder="请输入流程名称" clearable style="width: 240px"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" ref="refbut">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :data="tastList" :height="tableHeight" border>
      <!--      <el-table-column label="流程ID" align="center" prop="id"/>-->
      <el-table-column label="流程名称" align="center" prop="instanceName"/>
      <el-table-column label="任务节点名称" align="center" prop="name"/>
<!--      <el-table-column label="任务状态" align="center" prop="status"/>
      <el-table-column label="附件" align="center" prop="attachmentLink">
        <template slot-scope="scope"  v-if="scope.row.attachmentLink!=null">
          <a @click="preView(scope.row.attachmentLink)" target="_blank" class="buttonText" style="color: #00afff;text-decoration:underline">附件</a>
        </template>
      </el-table-column>-->
      <el-table-column label="办理人" align="center" prop="assignee"/>
      <el-table-column label="创建时间" align="center" prop="startTime"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button

            type="text"
            icon="el-icon-edit"
            @click="examineAndApprove(scope.row)"
            v-hasPermi="['archive:yijiaoDan:selectHandOverReceiveById']"
          >审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审批对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="500px" append-to-body>
      <leaveHistoryForm :businessKey="businessKey" v-if="'leave'==definitionKey"/>

      <el-form :model="form" ref="form" label-width="100px" class="demo-dynamic">
        <el-form-item
          v-for="(domain, index) in form.formData"
          :label="domain.controlLable"
          :key="index"
        >
          <el-radio-group v-model="domain.controlValue" v-if="'radio'==domain.controlType">
            <el-radio v-for="(defaults,indexd) in domain.controlDefault.split('--__--')"
                      :label=indexd
                      :key="indexd"
                      >{{defaults}}

            </el-radio>
            <!--<el-radio value="1" label="1" >同意</el-radio>
            <el-radio value="0" label="0" >不同意</el-radio>-->
          </el-radio-group>
          <el-input type="textarea" v-model="domain.controlValue" v-if="'textarea'==domain.controlType"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 其他审批单 -->
    <el-dialog title="审批" :visible.sync="otherOpen" v-if="otherOpen" width="500px" append-to-body>
      <el-form ref="otherForm" :model="otherForm" :rules="otherRules" label-width="120px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="审批意见：">
              <el-radio-group v-model="otherForm.resource">
                <el-radio :label="1">同意</el-radio>
                <el-radio :label="0">不同意</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="批 注：">
              <el-input v-model="otherForm.note" type="textarea" :rows="3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="closeDelDialog">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 档案移交审批单 -->
    <!-- isType:1 代表从待办任务打开的 -->
    <archiveHandOver :open="archiveHandOverOpen" @closeDelDialog="closeDelDialog" ref="archiveHandOverRef" @submitprint="submitprint" :printRow="archiveHandOverRow" :isType="2"></archiveHandOver>

  </div>
</template>
<style scoped>
.el-loading-mask {
  background-color: rgba(255, 255, 255, 0)!important;
}

</style>

<script>


import {formDataSave, listTask} from "@/api/archive/archiveTransfer/task";
import leaveHistoryForm from "@/views/workflow/leave/leaveHistoryForm";
import {getDicts_archive} from "@/api/archive/dict/dictManageData";

import archiveBorrow from "@/views/archive/archiveBorrow/archiveBorrowReceipt/notQrocessed/approve"; // 借阅审批单
import archiveSave from "@/views/archive/fileCollection/archiveApproval/notQrocessed/approve"; // 归档审批单
import archiveMjIdentify from "@/views/archive/archivalAppraisal/classification/examineApprove/detail"; // 密级审批单
import archiveHandOver from "@/views/archive/archiveTransfer/offline/printOrder.vue";
import estimateTableHeight from "@/mixins/estimateTableHeight";
//移交四性检测详情

  export default {
    name: "Leave",
    components: {leaveHistoryForm, archiveBorrow, archiveSave, archiveMjIdentify,archiveHandOver},
    mixins: [estimateTableHeight], // 混入方法
    data() {
      return {
        id:'',
        definitionKey: '',
        businessKey: '',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 请假表格数据
        tastList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {
          formData:[]
        },
        // 表单校验
        rules: {},
        // 流程字典
        flowPathOption: [],
        // 借阅审批
        archiveBorrowOpen: false,
        // 归档审批
        archiveSaveOpen: false,
        // 密级审批
        archiveMjIdentifyOpen: false,

        archiveHandOverOpen:false,
        // 其他审批
        otherOpen: false,
        otherForm: {
          resource: undefined,
          note: undefined
        },
        archiveHandOverRow:"",
        otherRules: {
          resource: [
            { required: true, message: "审批意见不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getOption()
    },
    methods: {
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查询请假列表 */
      getList() {
        this.loading = true;
        listTask(this.queryParams).then(response => {
          this.tastList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 获取流程定义字典
      getOption() {
        getDicts_archive('WORK_FLOW_KEY').then(response => {
          this.flowPathOption = response.data.map(item => {
            return {
              value: item.dictValue,
              label: item.dictLabel
            }
          })
        })
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.definitionKey = '',
          this.businessKey = '',
          this.form = {
            formData:[],
          };
        this.resetForm("form");
      },

      /** 审批按钮操作 */
      examineAndApprove(row) {
        this.reset();
        this.definitionKey = row.definitionKey;
        this.businessKey = row.businessKey;
        this.id=row.id;
        const obj = this.flowPathOption.find(item => item.label == row.definitionKey)

        if(obj == undefined) {
          this.otherOpen = true
        } else {
          const str = obj.value
          // 借阅
          if(str == 'archiveBorrow') {
            this.$nextTick(() => {
              this.$refs.archiveBorrowRef.init(row);
            })
            this.archiveBorrowOpen = true
          } else if(str == 'archiveSave') { // 归档
            this.$nextTick(() => {
              this.$refs.archiveSaveRef.init(row);
            })
            this.archiveSaveOpen = true
          } else if(str == 'archiveMjIdentify') { // 密级
            this.$nextTick(() => {
              this.$refs.archiveMjIdentifyRef.activeName = '0'
              this.$refs.archiveMjIdentifyRef.init(row);
            })
            this.archiveMjIdentifyOpen = true
          } else if(str == 'archiveHandOver') { // 密级
            this.$nextTick(() => {
              this.archiveHandOverRow = row.processVariables
              // this.$refs.archiveHandOverRef.init(row);
              this.archiveHandOverOpen = true
            })

          }
        }

      },
      closeDelDialog() {
        this.archiveBorrowOpen = false
        this.archiveSaveOpen = false
        this.archiveMjIdentifyOpen = false
        this.archiveHandOverOpen = false
        this.otherOpen = false
        this.queryParams.pageNum = 1
      },
      submitprint(){
        this.archiveBorrowOpen = false
        this.archiveSaveOpen = false
        this.archiveMjIdentifyOpen = false
        this.archiveHandOverOpen = false
        this.otherOpen = false
        this.queryParams.pageNum = 1
          this.getList();
      },
      submitChild(childForm) {
        this.otherForm = childForm;
        this.submit();
      },
      // 提交
      submit() {

        var data = []
        if(this.otherForm.resource == '1') {
          data = [{
            controlId: 'is_agree',
            controlDefault: '不同意--__--同意',
            controlIsParam: 'i',
            controlLable: '审批意见',
            controlType: 'radio',
            controlValue: 1
          }, {
            controlId: "comment",
            controlDefault: 'FormProperty_2ugo1se',
            controlIsParam: 'f--!!null',
            controlLable: '批注',
            controlType: 'textarea',
            controlValue: this.otherForm.note
          }];
          data.id = this.id
          formDataSave(this.id ,data).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.otherOpen = false
          })
        } else {
          data = [{
            controlId: 'is_agree',
            controlDefault: '不同意--__--同意',
            controlIsParam: 'i',
            controlLable: '审批意见',
            controlType: 'radio',
            controlValue: 0
          }, {
            controlId: "comment",
            controlDefault: 'FormProperty_2ugo1se',
            controlIsParam: 'f--!!null',
            controlLable: '批注',
            controlType: 'textarea',
            controlValue: this.otherForm.note
          }];
          data.id = this.id
          formDataSave(this.id ,data).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.otherOpen = false
          })
        }
        this.getList();
      },

      /** 提交按钮 */
      submitForm() {
        formDataSave(this.id,this.form.formData).then(response => {
          this.$modal.msgSuccess("审批成功");
          this.open = false;
          this.getList();
        });
        this.getList();
      },
      /**文档预览**/
      preView(filePath){
        console.info(filePath);
        if (null == filePath || '' == filePath) {
          this.$alert('申请人没有上传哦', '消息提示', {
            confirmButtonText: '确定',
          });
        }else {
          // let originUrl = 'http://127.0.0.1:80/ruoyi-admin/';   //要预览文件的访问地址'
          window.open(filePath);
        }
        // const bb = originUrl  + aa;
        // window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(bb)));
      },
    }
  };
</script>
